/**
*  \file min.h
*
*  \brief Minimum of two, three or four values
*
*  \author Abb K.  <kilian.abb@linde-mh.de>
*  \author Treisch J.  >joerg.treisch@linde-mh.de>
*
*  Return the minimum value of two, three or four values.
*
*
*/

#ifndef MIN_H_INCLUDED
#define MIN_H_INCLUDED

/*================================================[ public includes  ]================================================*/
#include "../cfg/prj.h"

/*================================================[ public defines   ]================================================*/

#define MIN2_GENERIC(a,b)                       \
   if ( a<b )                                   \
   {                                            \
      return a;                                 \
   }                                            \
   else                                         \
   {                                            \
      return b;                                 \
   }                                            \

#define MIN3_GENERIC(a,b,c)                     \
   if ( a<b )                                   \
   {                                            \
      if ( a<c )                                \
      {                                         \
         return a;                              \
      }                                         \
      else                                      \
      {                                         \
         return c;                              \
      }                                         \
   }                                            \
   else                                         \
   {                                            \
      if ( b<c )                                \
      {                                         \
         return b;                              \
      }                                         \
      else                                      \
      {                                         \
         return c;                              \
      }                                         \
   }                                            \

#define MIN4_GENERIC(a,b,c,d)                   \
   if ( a<b )                                   \
   {                                            \
      if ( c<d )                                \
      {                                         \
         if ( a<c )                             \
         {                                      \
            return a;                           \
         }                                      \
         else                                   \
         {                                      \
            return c;                           \
         }                                      \
      }                                         \
      else                                      \
      {                                         \
         if ( a<d )                             \
         {                                      \
            return a;                           \
         }                                      \
         else                                   \
         {                                      \
            return d;                           \
         }                                      \
      }                                         \
   }                                            \
   else                                         \
   {                                            \
      if ( c<d )                                \
      {                                         \
         if ( b<c )                             \
         {                                      \
            return b;                           \
         }                                      \
         else                                   \
         {                                      \
            return c;                           \
         }                                      \
      }                                         \
      else                                      \
      {                                         \
         if ( b<d )                             \
         {                                      \
            return b;                           \
         }                                      \
         else                                   \
         {                                      \
            return d;                           \
         }                                      \
      }                                         \
   }                                            \
/*================================================[ public datatypes ]================================================*/

/*================================================[ public variables ]================================================*/

/*================================================[ public functions ]================================================*/

/* Minimum of two values */
STATIC_INLINE U8  min2_u8  (U8  a, U8  b) { MIN2_GENERIC(a,b); }
STATIC_INLINE S8  min2_s8  (S8  a, S8  b) { MIN2_GENERIC(a,b); }
STATIC_INLINE U16 min2_u16 (U16 a, U16 b) { MIN2_GENERIC(a,b); }
STATIC_INLINE S16 min2_s16 (S16 a, S16 b) { MIN2_GENERIC(a,b); }
STATIC_INLINE U32 min2_u32 (U32 a, U32 b) { MIN2_GENERIC(a,b); }
STATIC_INLINE S32 min2_s32 (S32 a, S32 b) { MIN2_GENERIC(a,b); }
STATIC_INLINE F32 min2_f32 (F32 a, F32 b) { MIN2_GENERIC(a,b); }

/* Minimum of three values */
STATIC_INLINE U8  min3_u8  (U8  a, U8  b, U8  c) { MIN3_GENERIC(a,b,c); }
STATIC_INLINE S8  min3_s8  (S8  a, S8  b, S8  c) { MIN3_GENERIC(a,b,c); }
STATIC_INLINE U16 min3_u16 (U16 a, U16 b, U16 c) { MIN3_GENERIC(a,b,c); }
STATIC_INLINE S16 min3_s16 (S16 a, S16 b, S16 c) { MIN3_GENERIC(a,b,c); }
STATIC_INLINE U32 min3_u32 (U32 a, U32 b, U32 c) { MIN3_GENERIC(a,b,c); }
STATIC_INLINE S32 min3_s32 (S32 a, S32 b, S32 c) { MIN3_GENERIC(a,b,c); }
STATIC_INLINE F32 min3_f32 (F32 a, F32 b, F32 c) { MIN3_GENERIC(a,b,c); }

/* Minimum of four values */
STATIC_INLINE U8  min4_u8  (U8  a, U8  b, U8  c, U8  d) { MIN4_GENERIC(a,b,c,d); }
STATIC_INLINE S8  min4_s8  (S8  a, S8  b, S8  c, S8  d) { MIN4_GENERIC(a,b,c,d); }
STATIC_INLINE U16 min4_u16 (U16 a, U16 b, U16 c, U16 d) { MIN4_GENERIC(a,b,c,d); }
STATIC_INLINE S16 min4_s16 (S16 a, S16 b, S16 c, S16 d) { MIN4_GENERIC(a,b,c,d); }
STATIC_INLINE U32 min4_u32 (U32 a, U32 b, U32 c, U32 d) { MIN4_GENERIC(a,b,c,d); }
STATIC_INLINE S32 min4_s32 (S32 a, S32 b, S32 c, S32 d) { MIN4_GENERIC(a,b,c,d); }
STATIC_INLINE F32 min4_f32 (F32 a, F32 b, F32 c, F32 d) { MIN4_GENERIC(a,b,c,d); }

/*====================================================================================================================*/

#endif

/***********************************************************************************************************************
*                             Copyright 2012,2015 Linde Material Handling. All rights reserved.                             *
***********************************************************************************************************************/
